﻿using System;
using GeoAPI.Geometries;

namespace NetTopologySuite.Operation.Overlay.Snap
{
	public class SnapIfNeededOverlayOp
	{
		public static IGeometry Overlay(IGeometry g0, IGeometry g1, SpatialFunction opCode)
		{
			return new SnapIfNeededOverlayOp(g0, g1).GetResultGeometry(opCode);
		}

		public static IGeometry Intersection(IGeometry g0, IGeometry g1)
		{
			return SnapIfNeededOverlayOp.Overlay(g0, g1, SpatialFunction.Intersection);
		}

		public static IGeometry Union(IGeometry g0, IGeometry g1)
		{
			return SnapIfNeededOverlayOp.Overlay(g0, g1, SpatialFunction.Union);
		}

		public static IGeometry Difference(IGeometry g0, IGeometry g1)
		{
			return SnapIfNeededOverlayOp.Overlay(g0, g1, SpatialFunction.Difference);
		}

		public static IGeometry SymDifference(IGeometry g0, IGeometry g1)
		{
			return SnapIfNeededOverlayOp.Overlay(g0, g1, SpatialFunction.SymDifference);
		}

		public SnapIfNeededOverlayOp(IGeometry g1, IGeometry g2)
		{
			this._geom[0] = g1;
			this._geom[1] = g2;
		}

		public IGeometry GetResultGeometry(SpatialFunction opCode)
		{
			IGeometry result = null;
			bool flag = false;
			Exception ex = null;
			try
			{
				result = OverlayOp.Overlay(this._geom[0], this._geom[1], opCode);
				flag = true;
			}
			catch (Exception ex2)
			{
			}
			if (!flag)
			{
				try
				{
					result = SnapOverlayOp.Overlay(this._geom[0], this._geom[1], opCode);
				}
				catch (Exception)
				{
					throw ex;
				}
			}
			return result;
		}

		private readonly IGeometry[] _geom = new IGeometry[2];
	}
}
